<?php
namespace common\models\comment;

use common\models\goods\Goods;
use common\models\member\MemberInfo;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

/**
 * This is the model class for table "{{%comment}}".
 *
 */
class Comment extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%comment}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id','goods_id','message'], 'required'],
            [['member_id','goods_id','order_id','created_at'], 'integer'],
            [['message'], 'string', 'max' => 250],
            [['images'],'string','max'=>1000],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'comment_id' => 'ID',
            'member_id'=>'用户ID',
            'goods_id'=>'商品ID',
            'order_id' => '订单ID',
            'message' => '评论内容',
            'images' => '图片',
            'created_at' => '创建时间',
        ];
    }

    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'backendCreate' => ['goods_name', 'goods_code','goods_bar','goods_unit','gc_id','member_id','store_id'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }

    public function getMember(){
        return $this->hasOne(MemberInfo::className(),['id'=>'member_id'])
            ->select(['id','nickname','realname','head_portrait']);
    }

    public function getGoods()
    {
        return $this->hasOne(Goods::className(),['goods_id'=>'goods_id'])
            ->select(['goods_id','goods_name','goods_image','status','goods_storage','goods_price','goods_marketprice']);
    }

    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ];
    }
}
